<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\web\controller;

use app\web\controller\Base;

/**
 * 模板管理控制器
 */
class Template extends Base
{
    /**
     * 获取当前模板名称
     */
    public function getCurrentTemplate()
    {
        // 从配置或数据库获取当前模板
        $template = config('site.theme.template', 'default');
        return $template;
    }

    /**
     * 获取模板路径
     */
    public function getTemplatePath($view, $template = null)
    {
        if (!$template) {
            $template = $this->getCurrentTemplate();
        }

        // 检查模板文件是否存在
        $templatePath = "templates/{$template}/{$view}";
        $defaultPath  = $view;

        $templateFile = app()->getRootPath() . "app/web/view/{$templatePath}.html";

        if (file_exists($templateFile)) {
            return $templatePath;
        }

        // 回退到默认模板
        return $defaultPath;
    }

    /**
     * 渲染模板
     */
    public function renderTemplate($view, $data = [])
    {
        $templatePath = $this->getTemplatePath($view);
        return $this->render($templatePath, $data);
    }
}
